﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace GameVer1
{
    class VideoManager
    {
        Video video;
        VideoPlayer vidPlayer;
        double crop;
        double time;
        bool PlayFullVid;
        public Vector2 position;
        public Vector2 scale;
        bool loop;
        double timer;

        /// <summary>  
        /// Video manager lets you add a video and play and stop and such  
        /// </summary>  
        /// <param name="content">pass in content from game1.cs class</param>  
        /// <param name="videoFileLocation">full string file path</param>  
        /// <param name="Crop">take off time from the end of the vid, if the vid is 13 secs and you put 3 here you get the first 10 seconds of your video</param>  
        /// <param name="Position">position to play video, using 0,0 as origin</param>  
        /// <param name="Scale">scale of the video</param>  
        /// <param name="Loop">bool to tell weather or not to loop the video</param>  
        public VideoManager(ContentManager content, string videoFileLocation, double Crop, Vector2 Position, Vector2 Scale, bool Loop)
        {
            video = content.Load<Video>(videoFileLocation);
            loop = Loop;
            scale = Scale;
            position = Position;
            crop = Crop;

        }

        public void Start()
        {
            timer = 0;
            if (vidPlayer.State != MediaState.Playing)
                vidPlayer.Play(video);
        }

        public void Stop()
        {
            vidPlayer.Stop();
        }

        public void Pause()
        {
            if (vidPlayer.State == MediaState.Paused)
                vidPlayer.Resume();
            else if (vidPlayer.State == MediaState.Playing)
                vidPlayer.Pause();
        }

        /// <summary>  
        /// updates the player  
        /// </summary>  
        /// <param name="gameTime"></param>  
        public void Update(GameTime gameTime)
        {
            timer += gameTime.ElapsedGameTime.TotalSeconds;

            if (!loop)
                if (timer > video.Duration.TotalSeconds - crop)
                {
                    vidPlayer.Stop();
                    //Change state code  
                }
        }
        public void Draw(SpriteBatch spriteBatch, GameTime gameTime, Rectangle rectangle)
        {
            spriteBatch.Draw(vidPlayer.GetTexture(), rectangle, Color.White);

        }  
    }
}
